function authorClearOldDisplay() {
    $('#index-container').children().remove();
}

function authorDisplay(list) {
    for (var index = 0; index < list.length; ++index) {
        var info = list[index];

        var name = info[0];
        var id = info[1];
        var pinyin = info[2];

        name = "<a href='author/" + id + "'>" + name +  "</a>";

        // var row = "<tr><td>" + index + "</td><td>" + name + "</td><td>" + pinyin + "</td></tr>";
        var row = "<tr><td>" + index + "</td><td>" + name + "</td></tr>";

        $('#index-container').append(row);
    }
}

$(document).ready(function() {
    $.ajax({
        type: 'post',
        async: true,
        data: {"key": 'a'},
        url: document.location.origin + '/api/getauthorfrompinyin',
        dataType: 'text',
        error: function() {

        },
        success: function(data) {
            var json = JSON.parse(data);

            if (json.result == 200) {
                authorClearOldDisplay();
                authorDisplay(json.list);
            }
            else {

            }
        }
    });

    $('a[data-toggle="pill"]').on('shown.bs.tab', function (e) {
        var alphabet = $(e.target).text().toLowerCase();

        $.ajax({
            type: 'post',
            async: true,
            data: {"key": alphabet},
            url: document.location.origin + '/api/getauthorfrompinyin',
            dataType: 'text',
            error: function() {

            },
            success: function(data) {
                var json = JSON.parse(data);

                if (json.result == 200) {
                    authorClearOldDisplay();
                    authorDisplay(json.list);
                }
                else {

                }
            }
        });
    });
});